
import toast from '@/utils/toast'
import service from "@/utils/service";

const cloud={
  checkText:async (text)=>{
    if(process.env.TARO_ENV==='weapp'){
      toast.loading();
      wx.cloud.init({
        env: "heyi-1gyl85i7338d62f2",
        traceUser: true
      })
      const res:any = await wx.cloud.callFunction({
        name: 'checkTest',
        data: {
          text,
        }
      })
      toast.hide();
      if(res.result.code===200){
        return true
      }else{
        toast.info(res.result.msg)
        return false
      }
    }else{
      return true
    }
  },

  // url  1. string  2. string[]
  checkImage:async (url)=>{ //检测图片
    if(process.env.TARO_ENV==='weapp'){
      if(url){
        if(typeof(url)==='string'){
          url =  [url]
        }
      }
    let arr:any[] =[];
      url.map((item)=>{
        arr.push(service.checkImage({
          image_url:item
        }))
      })
      return Promise.all(arr).then((res:any) => {
        let bool = true;
        res.map((dataItem)=>{
          if(dataItem?.data?.result.suggestion==='pass'){

          }else{
            bool=false
          }
        })
        if(bool){
          return true
        }else{
          toast.info('图片包含敏感信息')
          return false
        }
      }).catch((err) => {
        console.error(err)
        toast.info('上传失败')
        return false
      })
    }else{
      return true
    }
  },

}



export default cloud;
